package com.lz.football.module.club
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.ClubEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.club.vo.TeamIconVo;
	import com.lz.football.module.club.item.BaseItem;
	import com.lz.football.module.club.item.LogoItem;
	import com.lz.football.module.club.type.ClothesColorMenuPanel;
	import com.lz.football.module.club.type.ColorMenuType;
	
	import common.BaseView.BaseView;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;
	import lz.controls.nick.PagePanel;

	/**
	 * 队徽
	 * 服务器初始队微ID 1101
1102
1103
1104
1105
1106
1107
1108
1201
1202
1203
1204
1205

	 * @author Administrator
	 * 
	 */	
	public class ClubLogoView extends BaseView
	{
		/**
		 *换页容器 
		 */
		private var pageContain:MovieClip;
		/**
		 *队徽容器 
		 */
		private var itemContain:MovieClip;
		/**
		 * 
		 */
		private var _page:PagePanel;
		/**
		 * 
		 */
		private var _itemVec:Vector.<LogoItem>;
		/**
		 *选中了item 
		 */
		private var _selectItem:LogoItem;
		/**
		 * 
		 */
		private var _logoarr:Array;
		/**
		 *效果容器 
		 */
		private var effContain:MovieClip;
		/**
		 * 
		 */
		private var _colorTab:ClothesColorMenuPanel;
		/**
		 *保存按钮  
		 */
		private var saveBtn:SimpleButton;
		/**
		 *选择的队徽ID 
		 */
		private var _selectLogoId:int;
		public function ClubLogoView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
//			_windowBg = Reflection.createInstance("com.lz.modul.club.logoBg");
//			this.addChild(_windowBg);
			symbolSkin = Reflection.createInstance("com.lz.modul.club.logo");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.logoBg;
			pageContain = symbolSkin.pageContain;
			itemContain = symbolSkin.itemContain;
			effContain = symbolSkin.effContain;
			saveBtn = symbolSkin.saveBtn;
			_colorTab = new ClothesColorMenuPanel();
			_colorTab.initUI(symbolSkin.colorTab);
			_colorTab.setSelectStatus(ColorMenuType.ONE_CONST);
			isDrag = true;
			isCloseBtn = true;
			super.initUI();
			moveClose(510,3);
			_logoarr = AppData.getInstance().clubDatamanager.teamIconType;
			initPage();
			initItem();
		}
		/**
		 * 
		 * 
		 */
		private function initPage():void
		{
			_page = new PagePanel();
			_page.initUI();
			_page.max = Math.ceil(_logoarr.length/9);
			_page.callBack = turnPage;
			pageContain.addChild(_page);
		}
		/**
		 *初始化item 
		 * 
		 */
		private function initItem():void
		{
			_itemVec = new Vector.<LogoItem>();
			var item:BaseItem;
			for(var i:int = 0;i < 9;i++)
			{
				item = new LogoItem();
				item.initUI("com.lz.modul.club.logoItem");
				itemContain.addChild(item);
				item.callBack = selectItem;
				item.x = (item.width + 5) * (i % 3);
				item.y = (item.height + 4) * int(i /3);
				_itemVec.push(item);
			}
			turnPage();
		}
		/**
		 *换页 
		 * @param $str
		 * 
		 */
		private function turnPage($str:String = null):void
		{
			clearItem();
			var item:BaseItem;
			var teamIconvo:TeamIconVo;
			var len:int;
			var num:int = (_page.min -1) *9
			len = _logoarr.length > _page.min * 9? 9: _logoarr.length - num;
			for(var i:int = 0;i < len;i++)
			{
				item = _itemVec[i];
				teamIconvo = _logoarr[num+i];
				item.data = teamIconvo;
				item.setImageSorce(teamIconvo.iconurl,70,70);
//				item.setScale(0.7);
			}
			selectItem(_itemVec[0]);
		}
		/**
		 *清除item图片 
		 * 
		 */
		private function clearItem():void
		{
			var item:BaseItem;
			for(var i:int = 0;i < 9;i++)
			{
				_itemVec[i].reset();
			}
		}
		/**
		 *选择了队徽 
		 * @param $item
		 * 
		 */
		private function selectItem($item:LogoItem):void
		{
			if($item.data == null)return;
			if(_selectItem != null)_selectItem.select = false;
			_selectItem = $item;
			_selectItem.select = true;
			onColorMuneHandler();
		}
			
		/**
		 *预览队徽效果 
		 * @param $teanVo
		 * 
		 */
		private function previewLogo($teanVo:TeamIconVo):void
		{
			DisplayObjectUtil.removeAllChildren(effContain);
			var mc:MovieClip = Reflection.createInstance($teanVo.iconurl);
			mc.scaleX = mc.scaleY = 1.5;
			mc.x = - mc.width >> 1;
			mc.y = -mc.height >> 1;
			effContain.addChild(mc);
			_selectLogoId = $teanVo.iconId
		}
		
		
		override public function addEvtListener():void
		{
			_colorTab.addEventListener(Event.CHANGE,onColorMuneHandler);
			saveBtn.addEventListener(MouseEvent.CLICK,onSaveHandler);
		}
		
		/**
		 *保存队徽 
		 * @param event
		 * 
		 */
		protected function onSaveHandler(event:MouseEvent):void
		{
			
			if(_selectItem == null)
			{
				Alert.showErrorTip("请选择更换的球衣");
				return;
			}
			saveBtn.removeEventListener(MouseEvent.CLICK,onSaveHandler);
			AppEventDispatcher.getInstance().dispatchEvent(new ClubEvent(ClubEvent.CHAGELOGO,{id:_selectLogoId}));
		}
		/**
		 * 更换队徽返回
		 * 
		 */
		public function getChangeLogo():void
		{
			saveBtn.addEventListener(MouseEvent.CLICK,onSaveHandler);
		}
		protected function onColorMuneHandler(event:Event = null):void
		{
			if(_selectItem == null || _selectItem.data == null)return;
			var vo:TeamIconVo = AppData.getInstance().clubDatamanager.getTeamIconId((_selectItem.data as TeamIconVo).type,_colorTab.selectType);
			previewLogo(vo);
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			_colorTab.removeEventListener(Event.CHANGE,onColorMuneHandler);
			saveBtn.removeEventListener(MouseEvent.CLICK,onSaveHandler);
		}
		override public function destroyView():void
		{
			removeEvtListener();
			if(_page != null)
				_page.destroyView();
			_page = null;
			while(_itemVec.length)
			{
				_itemVec.pop().destroyView();
			}
			_selectItem = null;
			_logoarr = null;
			_colorTab.destroyView();
			_colorTab = null;
			super.destroyView();
		}
	}
}

//override public function initUI(view:Object=null):void
//{
//	
//}
//override public function addEvtListener():void
//{
//	
//}
//override public function removeEvtListener():void
//{
//	
//}
//override public function destroyView():void
//{
//	
//}